package com.itzb.asone.fileserver.channel.qcloud;

import java.util.ArrayList;
import java.util.List;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.itzb.asone.fileserver.channel.IChannelService;
import com.itzb.asone.fileserver.model.FSFileInfo;
import com.itzb.asone.fileserver.model.FSResult;
import com.itzb.asone.system.service.IMessageService;
import com.itzb.asone.system.service.ISysParamService;
import com.itzb.asone.utils.SpringContextUtil;
import com.qcloud.cosapi.api.CosCloud;

public class QcloudChannelService extends IChannelService {
	
	private IMessageService messageService = SpringContextUtil.getBean("messageService");
	
	private ISysParamService sysParamService = SpringContextUtil.getBean("sysParamService");
	
	private CosCloud cos = null;
	
	
	// 默认获取的目录列表的数量
	private int DIR_NUM = 100;
    // 通过控制台提前建好的bucket
	private String bucketName = "asone";
    
	
    public QcloudChannelService(){
    	
    	int APP_ID = Integer.parseInt(sysParamService.getParamValue("QCLOUD_APP_ID"));
    	String SECRET_ID = sysParamService.getParamValue("QCLOUD_SECRET_ID");
    	String SECRET_KEY = sysParamService.getParamValue("QCLOUD_SECRET_KEY");
    	
    	this.DIR_NUM = Integer.parseInt(sysParamService.getParamValue("QCLOUD_DIR_NUM"));
    	this.bucketName = sysParamService.getParamValue("QCLOUD_BUCKET_NAME");
    	
    	 // 初始化cos
        cos = new CosCloud(APP_ID, SECRET_ID, SECRET_KEY);
        
    }
    
    /**
     * 获取目录
     * @return
     */
    public FSResult getFileList(String path){
    	try {
    		
			String result = cos.getFolderList(bucketName, path, DIR_NUM, "", 0,CosCloud.FolderPattern.File);
			return JsonToModel(result);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return null;
    	
    }
    
    
    /**
     * 目录删除
     * @param path
     * @return
     */
    public FSResult deleteFolder(String path){
    	try {
    		
			String result = cos.deleteFolder(bucketName,path);
			
			return JsonToModel(result);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return null;
    	
    }
    
    /**
     * 文件上传
     * @param path
     * @return
     */
	public FSResult uploadFile(String filePath,String fileName) {
		try {
    		
			String result = cos.uploadFile(bucketName, getUploadPath(fileName),filePath);
			JSONObject root = JSONObject.parseObject(result);
			FSResult fsResult = JsonToModel(result);
			if(fsResult.getSuccess()){
				FSFileInfo fsFileInfo = new FSFileInfo();
				fsFileInfo.setFileName(fileName);
				fsFileInfo.setFilePath(JSONObject.parseObject(root.get("data").toString()).get("access_url").toString());
				fsFileInfo.setFileUrl(JSONObject.parseObject(root.get("data").toString()).get("resource_path").toString());
				fsResult.getList().add(fsFileInfo);
			}
			return fsResult;
		} catch (Exception e) {
			e.printStackTrace();
		}
		return null;
	}
	
	/**
     * 文件删除
     * @param path
     * @return
     */
	public FSResult deleteFile(String filePath){
		try {
		
			String result = cos.deleteFile(bucketName, filePath);
			
			return JsonToModel(result);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return null;
	}
    
    
    private FSResult JsonToModel(String json){
    	FSResult fsResult= new FSResult();
    	
    	JSONObject root = JSONObject.parseObject(json);
    	
    	String code = root.get("code").toString();
    	String message = root.get("message").toString();
    	if(!code.equals("0")){
    		fsResult.isFailed();
    	} else {
    		fsResult.isSuccess();
    	}
    	
    	fsResult.setCode(code);
    	fsResult.setMessage(message);
    	
    	if(root.get("data") != null){
    		
    		List<FSFileInfo> list = new ArrayList<FSFileInfo>();
    		JSONObject data = new JSONObject();
        	data = JSONObject.parseObject(root.get("data").toString());
        	
        	if(data.get("infos")!=null){
        		JSONArray infos = JSONObject.parseArray(data.get("infos").toString());
        		for (Object object : infos) {
        			FSFileInfo fsFileInfo = new FSFileInfo();
        			JSONObject jsonFileInfo = JSONObject.parseObject(object.toString());
        			fsFileInfo.setFileName(jsonFileInfo.get("name").toString());
        			fsFileInfo.setFileSize(jsonFileInfo.get("filesize").toString());
        			fsFileInfo.setFilePath(jsonFileInfo.get("access_url").toString());
        			list.add(fsFileInfo);
				}
        		fsResult.setList(list);
        	}
    	}
    	
    	
    	return fsResult;
    }
    
    public static void main(String[] a) {
    	
    	FSResult fsResult= new FSResult();
    	
    	QcloudChannelService q = new QcloudChannelService();
    	
//    	fsResult = q.getFolderList();
    	
//    	fsResult = q.createFolder("/folder1/");
    	
//    	fsResult = q.getFolderStat("/folder1/");
    	
//    	fsResult = q.deleteFolder("/folder1/");
    	
    	fsResult = q.uploadFile("E:\\test.png", "test.png");
    	
//    	fsResult = q.deleteFile("/image/u0.png");
    	
    	
    	System.out.println(fsResult);
	}

	
}
